package com.copc.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.copc.common.util.CommonUtil;
import com.copc.copc.entity.MerchantConfig;
import com.copc.copc.service.IMerchantConfigService;
import com.copc.service.CopcService;
import io.swagger.annotations.Api;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;


/**
 * 1.授权登录接口
 * 2.查询用户券列表接口
 * 3.查询订单可用门店
 */
@Api("抖音接口")
@RestController
@RequestMapping("/isv")
@Log4j2
public class IsvController {

    @Autowired
    private CopcService copcService;

    @Autowired
    private IMerchantConfigService merchantConfigService;


    /**
     * 授权登录接口
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/code2Session")
    @ResponseBody
    public String code2Session(HttpServletRequest request){
        String code = request.getParameter("code");
        String appId = request.getParameter("app_id");
        if(CommonUtil.isNotEmpty(code)||CommonUtil.isNotEmpty(appId)){
            return "{\"err_no\":500,\"err_tips\":\"小程序已更新，请点击右上角“...”，重新进入小程序\",\"data\":{\"session_key\":\"\",\"openid\":\"\",\"anonymous_openid\":\"\",\"unionid\":\"\"}}";
        }
        return copcService.code2Session(code,appId);
    }

    /**
     * 查询用户券接口
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryCertificates")
    @ResponseBody
    public String queryCertificates(HttpServletRequest request){
        String appId = request.getParameter("app_id");
        String accountId = request.getParameter("account_id");
        String openId = request.getParameter("open_id");
        if(CommonUtil.isEmpty(accountId)||CommonUtil.isEmpty(appId)||CommonUtil.isEmpty(openId)){
            return "{\"err_code\":500,\"description\":\"参数有误\"}";
        }
        String page = request.getParameter("page");
        String pageSize = request.getParameter("page_size");
        return copcService.queryCertificates(appId,accountId,openId,page,pageSize);
    }

    /**
     * 查询订单可用门店地址
     * @param request
     * @return
     */
    @RequestMapping(value = "/orderCanUse")
    @ResponseBody
    public String orderCanUse(HttpServletRequest request){
        String appId = request.getParameter("app_id");
        String orderId = request.getParameter("order_id");
        if(CommonUtil.isEmpty(appId)||CommonUtil.isEmpty(orderId)){
            return "{\"err_code\":500,\"description\":\"参数有误\"}";
        }
        return copcService.orderCanUse(appId,orderId);
    }

    /**
     * 获取门店id
     * @param request
     * @return
     */
    @RequestMapping(value = "/getPoiid")
    @ResponseBody
    public String getPoiIdByAccountId(HttpServletRequest request){
        String account_id = request.getParameter("account_id");
        if(CommonUtil.isEmpty(account_id)){
            return "{\"err_code\":500,\"description\":\"参数有误\"}";
        }

        return copcService.getPoiId(account_id);
    }

    /**
     * 获取门店id
     * @return
     */
    @RequestMapping(value = "/updatePoiid")
    @ResponseBody
    public boolean updatePoiid(@RequestBody MerchantConfig merchantConfig){

        UpdateWrapper<MerchantConfig> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("open_shopid", "'"+merchantConfig.getOpenShopid()+"'").eq("account_id", merchantConfig.getAccountId());

        return merchantConfigService.update(updateWrapper);
    }


    @RequestMapping(value = "/usability")
    @ResponseBody
    public String usability(HttpServletRequest request){

        return "usability";
    }
}
